package com.example.ycdyus.dailyfruit.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.ycdyus.dailyfruit.BaseApp;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.ShopListViewAdater;
import com.example.ycdyus.dailyfruit.bean.Items;
import com.example.ycdyus.dailyfruit.bean.ShopBean;
import com.example.ycdyus.dailyfruit.constant.URLConstant;
import com.example.ycdyus.dailyfruit.db.Good;
import com.example.ycdyus.dailyfruit.ui.MainActivity;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */

public class ShopFragment extends Fragment implements ShopListViewAdater.GoodsChangeCallBack{

    private ListView mainShopLsitview;
    private ShopListViewAdater adapter;
    private List<Good> listItems = new ArrayList<>();
    private TextView totalMoney;
    private LinearLayout ll;
    private TextView haveGot;

    private Button goBackHome;

    private void initView(View view) {
        mainShopLsitview = (ListView) view.findViewById(R.id.lv_item_shop_lsitview);
        totalMoney = (TextView) view.findViewById(R.id.tv_item_shop_totalmoney);
        ll = (LinearLayout) view.findViewById(R.id.ll_shop_bottom);
        haveGot = (TextView) view.findViewById(R.id.main_shop_havesold);

        adapter = new ShopListViewAdater(getActivity(),listItems,R.layout.item_main_shop_listview);
        mainShopLsitview.setAdapter(adapter);
        adapter.setCallBack(this);
    }

    public ShopFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        initView(view);
        View emptyView = inflater.inflate(R.layout.shop_empty_layout, container, false);
        emptyView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        emptyView.setVisibility(View.GONE);
        goBackHome = (Button) emptyView.findViewById(R.id.btn_empty_layout);
        ((ViewGroup)mainShopLsitview.getParent()).addView(emptyView);
        mainShopLsitview.setEmptyView(emptyView);

        goBackHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApp.showToast("jjj");
                ((RadioButton) MainActivity.rg.getChildAt(0)).setChecked(true);
            }
        });

        initData();
        return view;
    }

    //请求网络数据
    private RequestParams getRequestParams(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign", "b221a584c2dbb41f96ec82579e58559b");
        params.addBodyParameter("timestamp", "1445424489");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "161c2ae532723d329ee711d284a1b36e");
        params.addBodyParameter("region_id", "106092");
        params.addBodyParameter("service", "cart.get");
        params.addBodyParameter("device_id", "1b8ba62bf241cee5b8c317d07c0c503e");
        params.addBodyParameter("carttmp", "");
        params.addBodyParameter("channel", "portal");
        params.addBodyParameter("version", "3.0.3");
        return params;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        try {
            ll.setVisibility(View.VISIBLE);
            haveGot.setVisibility(View.VISIBLE);
            if(listItems!=null&&!listItems.isEmpty()){
                listItems.clear();
            }else{
                ll.setVisibility(View.GONE);
                haveGot.setVisibility(View.GONE);
            }
            listItems.addAll(BaseApp.getInstance().getDbUtils().findAll(Good.class));
            adapter.notifyDataSetChanged();
            calcuMoney(listItems);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void calcuMoney(List<Good> list) {
        //计算总钱数
        int money = 0;
        for (int i = 0; i < list.size(); i++) {
            money += list.get(i).getNum()*Integer.parseInt(list.get(i).getSale_price());
        }
        totalMoney.setText(money+"");
    }

    @Override
    public void goodsChangeCallBack(int state) {
        try {
            List<Good> list = BaseApp.getInstance().getDbUtils().findAll(Good.class);
            if(listItems!=null&&!listItems.isEmpty()){
                listItems.clear();
                listItems.addAll(list);
                adapter.notifyDataSetChanged();
            }else{
                listItems.clear();
                adapter.notifyDataSetChanged();
            }
            calcuMoney(list);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
